﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Slja.BigFileUpload;
using Slja.Model;
using System.IO;
using FrameWork;

namespace Slja.WEB.Design
{
    public partial class upload : System.Web.UI.Page
    {
        public string Region
        {
            get { return Request.QueryString["region"]; }
        }
        public string Shop
        {
            get { return Request.QueryString["shop"]; }
        }
        public string PrjId
        {
            get { return Request.QueryString["prjid"]; }
        }//
        public string Id
        {
            get { return Request.QueryString["Id"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hdfID.Value = Request.QueryString["id"];
                if (string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    // Lib.JScript.MsgBox(Page, "缺少参数");return 
                    Response.Write("缺少参数:id");
                    Response.End();
                }
                else
                {
                    hdfID.Value = Request.QueryString["id"];
                }
                UploadContext context = UploadContextFactory.InitUploadContext(this, Request.MapPath("~/temp/")); // 指定临时目录
            }

        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            UploadContext context = UploadContextFactory.GetUploadContext();
            string FileName = context.GetFileNameByControl(File1.UniqueID);
           
            //-----------准备存放路径------------
            string vDir ="/UpLoadFile/Design/" + Region + "/";
            string CreatePath = Request.MapPath("~/") + vDir;
            string vFileName= Shop + "(" + PrjId + ")_" + FileName;
            string vPath = vDir + vFileName;
            //string FullPath = Server.MapPath("/") + vPath;
            if (!Directory.Exists(CreatePath))
            {
                Directory.CreateDirectory(CreatePath);
            }

            //-------------------------------------------
            if ((context != null) && (context.Status == uploadStatus.Complete))
            {
                //string physiPath = filePath + FileName;
                if (context.SaveFile(File1.UniqueID, CreatePath + vFileName))
                {
                    UploadContextFactory.Release();//释放资源 删除临时文件
                    //(--修改记录：不释放缓存对象，排除bug，利用自动过期机制)

                    //添加数据库记录
                    PrjDesignFilepack model = new PrjDesignFilepack();
                    model.CreaterID = Common.Get_UserID;
                    model.FilepackName = FileName;
                    model.FilepackPath = vPath;
                    model.FilepackSize = context.TotalLength;
                    model.Memo = tbxMemo.Text.Trim();
                    model.PID = int.Parse(hdfID.Value);
                    model.UploadTime = DateTime.Now;

                    BLL.PrjDesignFilepack bll = new BLL.PrjDesignFilepack();
                    bll.Add(model);
                }
            }
        }
    }
}